// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "ItemsListWidget.h"

const int itemPointerRole = Qt::UserRole;

ItemsListWidget::ItemsListWidget(const Item *root, QWidget *parent) :
    QTreeWidget(parent),
    m_root(0)
{
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);

    connect (this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(onCurrentChanged()));
    connect (this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(onItemChanged(QTreeWidgetItem*, int)));
    setColumnCount(2);
    QStringList headers;
    headers << "Items" << "Visible";
    setHeaderLabels(headers);
    setRootItem(root);
    header()->setStretchLastSection(false);
    header()->setSectionResizeMode(0, QHeaderView::Stretch);
    header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
}

void ItemsListWidget::setRootItem(const Item *root)
{
    delete takeTopLevelItem(0);
    m_root = root;

    QTreeWidgetItem* rootTreeItem = createTreeItem(root);
    if (rootTreeItem == 0) {
        return;
    }

    insertTopLevelItem(0, rootTreeItem);
    rootTreeItem->setExpanded(true);
}

const Item *ItemsListWidget::getCurrentItem() const
{
    QTreeWidgetItem* treeItem = currentItem();
    if (treeItem == 0) {
        return 0;
    }
    return treeItem->data(0, itemPointerRole).value<const Item*>();
}

void ItemsListWidget::setCurrent(const Item *item)
{
    QTreeWidgetItemIterator it(this);
    while (*it) {
        QTreeWidgetItem *listItem = (*it);
        const Item *i = listItem->data(0, itemPointerRole).value<const Item*>();
        if (i == item) {
            listItem->setSelected(true);
            setCurrentItem(listItem);
            scrollToItem(listItem);
        }
        it++;
    }
}

QTreeWidgetItem *ItemsListWidget::createTreeItem(const Item *item) const
{
    if (item == 0) {
        return 0;
    }

    QTreeWidgetItem* parentTreeItem = new QTreeWidgetItem(QStringList(item->getName()));
    parentTreeItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    parentTreeItem->setCheckState(1, Qt::Checked);
    parentTreeItem->setData(0, itemPointerRole, QVariant::fromValue<const Item*>(item));
    foreach (Item* child, item->getChildren()) {
        QTreeWidgetItem* childTreeItem = createTreeItem(child);
        parentTreeItem->addChild(childTreeItem);
    }

    return parentTreeItem;
}

void ItemsListWidget::onCurrentChanged()
{
    emit currentItemChanged(getCurrentItem());
}

void ItemsListWidget::onItemChanged(QTreeWidgetItem *item, int column)
{
    if (column == 1) {
        Item* i = const_cast<Item*>(item->data(0, itemPointerRole).value<const Item*>());
        if (i) {
            i->setVisible(item->checkState(1) == Qt::Checked);
            emit itemVisibleChanged();
        }
    }
}


